{include file="public/layout" /}
<body class="laytp-container">
<div class="layui-card">
	<div class="layui-card-body">
		<div class="layui-fluid">
		<div class="layui-row layui-col-space15">
		  <div class="layui-card layui-col-md12">
					<div class="layui-card-body" pad15>
						 <div style="padding:20px 10px;font-size:16px">
							 {if $auth_data && $auth_data['type'] == 1}
							 <div style="padding:10px 0;color:red;">您当前为临时授权版本，有效期截止：{$auth_data['end_time']}</div>
							 {/if}

							<div style="padding:10px 0;color:#333;">
								您的当前版本为：V{$version}
								{if $update_data && $update_data['version']}
									最新版本为 V{$update_data['version']}，更新时间：{$update_data['createtime']}，更新说明：{$update_data['remark']}
								{/if}
							</div>
							{if $remark}<div style="padding:20px 0;"><span style="color:#333;height:40px;line-height:40px">V{$newversion} 版本说明：</span><br>{:nl2br($remark)}</div>{/if}
							<button class="layui-btn layui-btn-normal" style="margin-top:20px" onclick="getfilllist()">检查更新</button>
							<button class="layui-btn layui-btn-primary" style="margin-top:20px" onclick="updatesql()">升级数据库</button>
						 </div>
						{if $update_data && $update_data['version']}
						更新内容：<div style="padding:20px 0;">{:html_entity_decode($update_data['update_text'])}</div>
						{/if}
					</div>
				</div>
			</div>
	  </div>
		<div style="display:none;position:absolute;top:20px;height:50px;padding-left:20px;line-height:50px;width:700px;left:50%;margin-left:-350px;background:#555;color:#fff;border-radius:5px;z-index:99891015;font-size:16px" id="upfiledialog"></div>
	</div>
</div>
	<script>
	var numindex = 0;
	function getfilllist(){
		layer.confirm('更新前请先备份！！！关闭服务器防火墙、防篡改等安全软件防止误拦截！',{icon: 7, title:'操作确认'}, function(index){
			//var index = layer.load();
			var loadingdialog = layer.open({type:1,content:'<div style="padding:10px 5px"><div class="flex-y-center"><img src="/static/images/loading.gif"/><span style="font-size:20px;padding-left:15px">正在对比升级文件...</span></div></div>',area:'300px',title:false,closeBtn:false});
			$.ajax({
				url:"{:url('getdiflist')}",
				type:'POST',
				data:{},
				success:function(res){
					layer.close(loadingdialog);
					if(res.status != 1){
						dialog(res.msg?res.msg:'网络请求超时，请重试',res.status);return;
					}
					var newfileArr = res.new
					var modifyfileArr = res.modified
					var allfile = res.allfiles;
					var html = '';
					if(newfileArr.length > 0){
						html += '<div style="color:#000">新增的文件：</div>';
						html += '<div style="color:green">';
						for(var i in newfileArr){
							html += newfileArr[i]['path'] + '<br/>';
						}
						html += '</div>';
					}
					if(modifyfileArr.length > 0){
						html += '<div style="color:#000">变更的文件：</div>';
						html += '<div style="color:red">';
						for(var i in modifyfileArr){
							html += modifyfileArr[i]['path'] + '<br/>';
						}
						html += '</div>';
					}
					console.log(allfile);

					if(newfileArr.length == 0 && modifyfileArr.length == 0){
						dialog('暂无更新');
					}else{
						var querenlayer = layer.open({title:'升级操作确认',area:['600px','400px'],type:0,content:html,btn:['确认升级','取消'],yes:function(){
								layer.close(querenlayer);
								numindex = 0;
								$('#upfiledialog').show();
								updatefile(allfile);
							}})
					}
				},error:function(res){
					layer.close(index)
					layer.open({type:1,title:'升级失败，请重试',area:['auto','80%'],content:res.responseText})
					console.log(res.responseText)
				}
			})
		});
	}
	function updatefile(allfile){
		$('#upfiledialog').html('<img src="/static/images/loading.gif" style="width:30px;height:30px"/>&nbsp;&nbsp;'+numindex+'/'+allfile.length+'&nbsp;&nbsp;正在更新文件：'+allfile[numindex]['path']);
		$.post("{:url('download')}",{ file_path:allfile[numindex]['path'],md5:allfile[numindex]['md5'],test:1},function(data){
			numindex++;
			if(numindex < allfile.length){
				updatefile(allfile);
			}else{
				$('#upfiledialog').html('<img src="/public/static/images/loading.gif" style="width:30px;height:30px"/>&nbsp;&nbsp;正在更新数据库...');
				$.post("{:url('update_db')}",{},function(res){
					layer.alert('升级成功',{icon: 1},function(){
						reload();
					});
				})
			}
		})
	}
	function updatesql(){
		$.post("{:url('update_db')}",{},function(res){
			dialog(res.msg)
		});
	}
  </script>
</body>
</html>